/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.finance.api;

import com.lxk.finance.domain.bo.AccUserAccountBo;
import com.lxk.finance.domain.vo.AccUserAccountVo;
import com.lxk.finance.service.IAccUserAccountService;
import com.lxk.service.core.Result;
import com.lxk.service.finance.api.WalletServiceApi;
import com.lxk.service.finance.request.QueryAccountParam;
import com.lxk.service.finance.request.UpdateAccountParam;
import com.lxk.service.finance.response.UserAccountDto;
import lombok.AllArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.ValidationUtils;
import org.springframework.stereotype.Service;

/**
 * 用户账户内部API
 *
 * @author huahua
 * 2024/8/22
 */
@Service
@AllArgsConstructor
public class WalletServiceApiImpl implements WalletServiceApi {
    private final IAccUserAccountService accUserAccountService;

    /**
     * 根据用户ID和账户类型 查询对应账户的余额
     *
     * @param param
     * @return
     */
    @Override
    public Result<UserAccountDto> queryByParam(QueryAccountParam param) {
        ValidationUtils.validate(param);
        AccUserAccountVo accountVo = accUserAccountService.findByUserIdAndAccountType(param.getUserId(), param.getAccountType());
        UserAccountDto dto = MapstructUtils.convert(accountVo, UserAccountDto.class);
        return Result.ok(dto);
    }

    /**
     * 增加账户金额
     *
     * @param param
     * @return
     */
    @Override
    public Result<Boolean> add(UpdateAccountParam param) {
        ValidationUtils.validate(param);
        return Result.ok(accUserAccountService.addAccount(param.getUserId(), param.getUserType(),
            param.getOrderSn(), param.getAmount(),
            AccUserAccountBo.AccountType.getByCode(param.getAccountType()), param.getExtraInfo()));
    }

    /**
     * 减少账户金额
     *
     * @param param
     * @return
     */
    @Override
    public Result<Boolean> reduce(UpdateAccountParam param) {
        ValidationUtils.validate(param);
        return Result.ok(accUserAccountService.reduceAccount(param.getUserId(), param.getUserType(),
            param.getOrderSn(), param.getAmount(),
            AccUserAccountBo.AccountType.getByCode(param.getAccountType()), param.getExtraInfo()));
    }
}
